/*
 * GeoArticle Model Classes
 *
 * Copyright 2010 Ivar Bergman (http://ivarbergman.se)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */



/* ------------------------------------------------------------ */
/* ArticleMarker class
 *
 * Extends the LabeledMarker class
 */
function ArticleMarker(lat, lng, id, title, categories, icon){

  this.labelTitle = title || "";
  this.categories = categories || [];
  this.hideState = true;
  this.active = [];
  this.open = false;


  options = [];
  options.id = id;
  options.clickable = true;
  options.labelOffset = new GSize(5, -29);
  options.title = title;

  if (icon)
    {
      options.icon = icon;
    }

  var point = new GLatLng(lat, lng);
  LabeledMarker.apply(this, [point, options]);
  GA.trigger("addMarker", [this]);

}

ArticleMarker.prototype = new LabeledMarker(new GLatLng(0, 0), []);


ArticleMarker.prototype.hideCategory = function(id) {
  if (this.active[id] != undefined)
  {
    delete this.active[id];
  }
  if (array_size(this.active) == 0)
  {
    this.hide();
    this.hideState = true;
  }
};


ArticleMarker.prototype.showCategory = function(id) {
  if (array_has(this.categories, id))
  {
    this.active[id] = true;
  }
  this.show();
  this.hideState = false;

};

/* ------------------------------------------------------------ */
/* ArticleCategory class
 *
 * Models the relation between a category and its articles.
 */
function ArticleCategory(id, title)
{
  this.markers = [];
  this.id = id;
  this.title = title;
  GA.trigger("addCategory", [this]);
}

ArticleCategory.prototype.show = function()
{
  for (var idx = 0 ; idx < this.markers.length; idx++)
  {
    m = this.markers[idx];
    m.showCategory(this.id);
  }
  GA.trigger("showCategory", [this]);
};

ArticleCategory.prototype.hide = function()
{
  for (var idx = 0 ; idx <this.markers.length; idx++)
  {
    m = this.markers[idx];
    m.hideCategory(this.id);
  }
  GA.trigger("hideCategory", [this]);
};


